//package com.winhxd.b2c.admin.common.aspect;
//
//import net.huixiadan.common.domain.constants.LoggerPrefixes;
//import net.huixiadan.common.domain.web.ResponseResult;
//import net.huixiadan.common.domain.web.ResponseSubCode;
//import net.huixiadan.common.i18n.service.MessageSourceService;
//import net.huixiadan.common.lang.ExceptionUtil;
//import org.apache.commons.lang3.StringUtils;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Locale;
//
///**
// * ControllerMethodAdvice
// *
// * @author yindanqing
// * @date 2018/7/5 17:37
// * @Description:
// */
//@Aspect
//@Component
//public class ControllerMethodAdvice {
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(ControllerMethodAdvice.class);
//
//    private static final String SEPARATOR = " ";
//
//    private static final String ERROR_MSG = "";
//
//    @Autowired
//    private MessageSourceService messageSourceService;
//
//    @Pointcut(value = "@within(org.springframework.web.bind.annotation.RestController)")
//    public void restControllerAspect(){}
//
//    @Pointcut(value = "@within(org.springframework.stereotype.Controller)")
//    public void controllerAspect(){}
//
//    @AfterReturning(pointcut = "restControllerAspect() || controllerAspect() ", returning="rvt")
//    @Before()
//    public void doAfter(JoinPoint joinPoint, Object rvt) {
//        if((joinPoint.getSignature().getModifiers() & 1) != 1){
//            LOGGER.error(LoggerPrefixes.APPLICTION + getMethodName(joinPoint) + "method modifiers error.");
//            return;
//        }
//        if(null == rvt){
//            LOGGER.error(LoggerPrefixes.APPLICTION + getMethodName(joinPoint) + "method error.");
//            return;
//        }
//        if(!(rvt instanceof ResponseResult)){
//            return;
//        }
//        ResponseResult result = (ResponseResult) rvt;
//        if (ResponseSubCode.CODE_OK == result.getSubCode()) {
//            return;
//        }
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        setSubMessage(result, String.valueOf(result.getSubCode()), request.getLocale());
//        LOGGER.warn("{}业务接口调用异常:[{}]:{}", LoggerPrefixes.BUSINESS_EXCEPTION_ADVICE, result.getSubCode(), result.getSubMessage());
//    }
//
//    private String getMethodName(JoinPoint joinPoint){
//        return SEPARATOR + joinPoint.getSignature().toString().split(SEPARATOR)[1] + SEPARATOR;
//    }
//
//    private void setSubMessage(ResponseResult result, String subCode, Locale locale) {
//        try {
//            if(StringUtils.isNotBlank(result.getSubMessage())){
//                return;
//            }
//            String subMessage = messageSourceService.getMessageWithLocale(subCode, locale);
//            if(StringUtils.isNotBlank(subMessage)){
//                result.setSubMessage(subMessage);
//            } else{
//                result.setSubMessage(messageSourceService.getMessageWithLocale("10000", locale));
//            }
//        } catch (Exception ex) {
//            String trace = ExceptionUtil.parseStackTrace(ex);
//            LOGGER.error("{}异常编码未定义，请及时将异常编码 [{}] 添加到i18n/codes.properties中\n{}",
//                    LoggerPrefixes.BUSINESS_EXCEPTION_ADVICE, result.getSubCode(), trace);
//        }
//    }
//
//}
